<?php
/**
 * @property  UserModel $mUser
 */

namespace app\index\model;

use app\common\RedisDB;
use app\common\DMutex;
use GatewayClient\Gateway;
use think\log\driver\Socket;

class NoticeModel extends BaseModel
{
    const LIST_KEY = 'noticelist';
    const INFO_KEY = 'notice:';
    const TIMER_DELAY = 16;//后端延迟16s

    const TABLE_NOTICE="notice";
    const TABLE_USERNOTICE="usernotice";

    //通知状态
    const STATE_NOREAD = 0;
    const STATE_READ = 1;

    private $mUser = null;
    private $uid = null;
    private $info = null;
    private $noticelist=null;

    public function __construct($user)
    {
        $this->mUser=$user;
        if($user)
        $this->uid= $this->mUser->getUid();
        $this->redis = RedisDB::factory('notice');
    }

    public function read($noticeid)
    {
        $notice=db(self::TABLE_USERNOTICE)->where("noticeid=$noticeid and openid='{$this->uid}'")->find();
        if($notice)
        {
            $notice['bread']=1;
            $notice['readtime']=date("Y-m-d H:i:s", time());
            db(self::TABLE_USERNOTICE)->where("noticeid=$noticeid and openid='{$this->uid}'")->update($notice);
        }
    }

    public function add($notice,$type,$uids)
    {
        if($nid=db(self::TABLE_NOTICE)->insertGetId($notice))
        {
            if($type==0)
            {
                //全局通知
                $users = db('user')->field('openid')->select();
                $usernoticelist=array();
                $usernotice = array();
                foreach ($users as $v) {
                    $usernotice['noticeid'] = $nid;
                    $usernotice['openid'] = $v['openid'];
                    $usernotice['readtime'] = date('Y-m-d H:i:s',time());
                    $usernoticelist[]=$usernotice;
                }
                db(self::TABLE_USERNOTICE)->insertAll($usernoticelist);
            }
            else if($type==1)
            {
                $uidarray = json_decode($uids);
                print_r($uidarray);
                $usernoticelist=array();
                $usernotice = array();
                foreach ($uidarray as $v) {
                    $usernotice['noticeid'] = $nid;
                    $usernotice['openid'] = $v;
                    $usernotice['readtime'] = date('Y-m-d H:i:s',time());
                    $usernoticelist[]=$usernotice;
                }
                db(self::TABLE_USERNOTICE)->insertAll($usernoticelist); 
            }
            else if($type==2) {
                $usernotice['noticeid'] = $nid;
                $usernotice['openid'] = $uids;
                $usernotice['readtime'] = date('Y-m-d H:i:s',time());
                db(self::TABLE_USERNOTICE)->insert($usernotice);
            }
        }
    }

    public function getUserNoticelist()
    {
        if($this->noticelist==null)
        {
            $this->noticelist = db(self::TABLE_NOTICE)->where("noticetype=0")->select();
//            $this->noticelist = db(self::TABLE_USERNOTICE)->where("openid='{$this->uid}'")->select();
            foreach($this->noticelist as $k=>$v)
            {
                //$notice=db(self::TABLE_NOTICE)->where("id={$v['noticeid']}")->find();
                $this->noticelist[$k]['data']=$v;
                $this->noticelist[$k]['bread']=0;
            }
        }
        return $this->noticelist;
    }

    public function getNoreadCount()
    {
        db(self::TABLE_USERNOTICE)->where("userid={$this->uid} and bread=0")->select();
    }
}